package com.si.anddos.search;

import java.util.ArrayList;

import com.si.anddos.R;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SearchResultAdapter extends ArrayAdapter<SearchResult> {	
	private ArrayList<SearchResult> searchResultDataItems;
	
	private Activity context;
	
	public SearchResultAdapter(Activity context, int textViewResourceId, ArrayList<SearchResult> searchResultDataItems) {
        super(context, textViewResourceId, searchResultDataItems);
        this.context = context;
        this.searchResultDataItems = searchResultDataItems;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
    	
		View view = convertView;
		if (view == null) {
			LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(R.layout.search_row, null);
		}
		
		SearchResult searchResult = searchResultDataItems.get(position);
		
		if (searchResult != null) {
			
			TextView textSearchResultName = (TextView) view.findViewById(R.id.textSearchResultName);
			textSearchResultName.setText(searchResult.getName());
			
			TextView textSearchResultAddress = (TextView) view.findViewById(R.id.textSearchResultAddress);
			textSearchResultAddress.setText(searchResult.getAddress());
			
			Resources res = context.getResources();
			
			TextView textSearchResultSize = (TextView) view.findViewById(R.id.textSearchResultSize);
			textSearchResultSize.setText(String.format(res.getString(R.string.textSearchRowSize), searchResult.getSize()));		
			TextView textSearchResultExtension = (TextView) view.findViewById(R.id.textSearchResultExtension);
			textSearchResultExtension.setText(String.format(res.getString(R.string.textSearchRowExtension), searchResult.getExtension()));
			TextView textSearchResultAdded = (TextView) view.findViewById(R.id.textSearchResultAdded);
			textSearchResultAdded.setText(String.format(res.getString(R.string.textSearchRowAdded), searchResult.getAdded()));
			
			
			//If the url is possible to download put it on green
			//Else put it on red
			if(searchResult.getAddress().contains("rapidshare.com"))
				view.setBackgroundColor(Color.GREEN);
			else
				view.setBackgroundColor(Color.RED);
		}
		
		return view;
		
	}
	
	
	
	
}

